﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmBoxOrder : Form
    {
        public FrmBoxOrder()
        {
            InitializeComponent();
        }
        private static List<BoxOrderInfo> _lstBoxOrder = null;
        public static List<BoxOrderInfo> lstBoxOrder
        {
            get
            {
                if (_lstBoxOrder == null) _lstBoxOrder = new List<BoxOrderInfo>();
                return _lstBoxOrder;
            }
        }
        private static BoxOrderInfo _BoxOrder = new BoxOrderInfo();
        private void FrmBoxOrder_Load(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (lstBoxOrder != null && lstBoxOrder.Count > 0)
                {

                    var re = Duty.BoxOrderUIRule.NewInstance.SetBoxOrder(LoginBean.Instance.USERCODE, lstBoxOrder);


                    if (re.Params)
                    {
                        _lstBoxOrder = null;
                        this.BoxOrderSource.DataSource = null;
                        this.dbGridBoxOrder.DataSource = this.BoxOrderSource;
                        this.dbGridBoxOrder.Refresh();
                        this.txtBoxCode.Text = string.Empty;
                        this.txtOrderCode.Text = string.Empty;
                        this.txtBoxCode.Focus();
                    }
                    else
                    {
                        MessageBox.Show(re.Message);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void txtBoxCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                string BoxCode = this.txtBoxCode.Text.Trim();
                if (string.IsNullOrEmpty(BoxCode))
                {
                    MessageBox.Show("请扫描箱号条码！");
                    this.txtBoxCode.Focus();
                    return;
                }

                Cursor.Current = Cursors.WaitCursor;
                _BoxOrder.BOXCODE = BoxCode;
                this.txtOrderCode.Focus();
            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);

            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void txtOrderCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            try
            {
                string BoxCode = this.txtBoxCode.Text.Trim();
                if (string.IsNullOrEmpty(BoxCode))
                {
                    MessageBox.Show("请先扫描箱号条码！");
                    this.txtBoxCode.Focus();
                    return;
                }
                string OrderCode = this.txtOrderCode.Text.Trim();
                if (string.IsNullOrEmpty(OrderCode))
                {
                    MessageBox.Show("请扫描订单编码！");
                    this.txtOrderCode.Focus();
                    return;
                }
                if (!OrderCode.StartsWith("W") && !OrderCode.StartsWith("SOA") && !OrderCode.StartsWith("I") && !OrderCode.StartsWith("A"))
                {
                    MessageBox.Show("请扫描正确的订单编码！");
                    this.txtOrderCode.Text = string.Empty;
                    this.txtOrderCode.Focus();
                    return;
                }

                Cursor.Current = Cursors.WaitCursor;
                _BoxOrder.ORDERCODE = OrderCode;
                if (lstBoxOrder.Select(_ => _.BOXCODE == _BoxOrder.BOXCODE & _.ORDERCODE == _BoxOrder.ORDERCODE).Count() <= 0)
                {
                    lstBoxOrder.Add(_BoxOrder);
                }
                this.BoxOrderSource.DataSource = lstBoxOrder;
                this.dbGridBoxOrder.DataSource = this.BoxOrderSource;
                this.dbGridBoxOrder.Refresh();
                _BoxOrder = new BoxOrderInfo();
                this.txtBoxCode.Text = string.Empty;
                this.txtOrderCode.Text = string.Empty;
                this.txtBoxCode.Focus();

            }

            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        private void FrmBoxOrder_Closed(object sender, EventArgs e)
        {
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
            //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
            //Object o = Activator.CreateInstance(type);
            //System.Reflection.MethodInfo mi = type.GetMethod("Show");

            //mi.Invoke(o, null);

            this.Close();
            this.Dispose();
        }

        private void FrmBoxOrder_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (MessageBoxEx.Question("确定退出吗？") == DialogResult.Yes)
                {
                    //System.Reflection.Assembly ass = System.Reflection.Assembly.Load("WoChu.RfClient.Login");
                    //Type type = ass.GetType("WoChu.RfClient.Login.FrmMain");
                    //Object o = Activator.CreateInstance(type);
                    //System.Reflection.MethodInfo mi = type.GetMethod("Show");

                    //mi.Invoke(o, null);

                    this.Close();
                    this.Dispose();
                }
            }
        }
    }
}